﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;

namespace rndbuddy_csourcer
{
    static class server_pipe
    {
        static string server = "http://rndbuddy.com/";
        static string gw_crowdsourcer = "api/crowdsourcer/report/";

        
        public static void report_crowdsourcer_data(bool reset_collection)
        {
            string data;
            data = crowdsourcer.merge_and_report(reset_collection);
            data = Crypto.EncodeTo64(data);

            HttpPost(server + gw_crowdsourcer,
                        "apikey=" + settings.api_key +
                        "&data="+data
                    );
        }

        public static string HttpPost(string URI, string Parameters)
        {
            try
            {
                System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
                req.Proxy = null; //disables windows proxy, uncomment to rely on default WebRequest Behaviour
                req.ContentType = "application/x-www-form-urlencoded";
                req.Method = "POST";
                byte[] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
                req.ContentLength = bytes.Length;
                System.IO.Stream os = req.GetRequestStream();
                os.Write(bytes, 0, bytes.Length);
                os.Close();
                System.Net.WebResponse resp = req.GetResponse();
                if (resp == null) return null;
                System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
                return sr.ReadToEnd().Trim();
            }
            catch (Exception e)
            {
                rndbuddy_csourcer.log("Error in Http Comunication to Server. Error is " + e.Message);
                rndbuddy_csourcer.log(e.StackTrace);
                return "false;error in Server communication";
            }
            finally
            {
                //...
            }
        }



    }
}
